#!/bin/bash

########################################################################
#This scripts is checking your host system: the cpu ,i/o,mem,network,
#processes stat.
#  vision 1.0 Bate
########################################################################

cd /data/monitor

#make the local language is chinese
#export LANG=zh_CN


logdate=`date +%m%d`
removedate=`date +%d`
curdate=`date +%H`
logdir=/data/monitor
logcpuuse=$logdir/cpuuse_$logdate.log
logcpuload=$logdir/cpuload_$logdate.log
logioload=$logdir/ioload_$logdate.log
lognetworkload=$logdir/networkload_$logdate.log
logsysprocess=$logdir/sysprocess_$logdate.log
logmemuse=$logdir/memuse_$logdate.log
lognetsession=$logdir/netsession_$logdate.log
logmysqlprocess=$logdir/mysqlprocess_$logdate.log


################################
#统计cpu的使用率信息
################################

#判断文件是否存在
if [ -f $logcpuuse ]
then
#echo "logfile is exist !"
if [ "$curdate" == "23" ]
then
date +"%D %r" >>$logcpuuse
sar -u 2 1 |grep all |head -1 >> $logcpuuse
else
sar -u 2 1 |grep all |head -1 >> $logcpuuse
fi
else
#touch $logfile

date > $logcpuuse
sar -u 2 2 | grep CPU >> $logcpuuse

fi

###############################
#统计cpu的负载信息
###############################

if [ -f $logcpuload ]
then
#echo "logfile is exist !"

uptime  >> $logcpuload
else
#touch $logfile
date > $logcpuload
uptime >> $logcpuload
fi


##############################
#磁盘io的负载的信息
##############################

if [ -f $logioload ]
then
date +"%D %r" >> $logioload
iostat -m -xd 1 1 | sed -n '2,$p' >> $logioload

else
date +"%D %r" > $logioload
iostat -m -xd 1 1 | sed -n '2,$p' >> $logioload
fi


#################################
#统计网络负载的信息
################################

if [ -f $lognetworkload ]
then
date +"%D %r" >> $lognetworkload
sar -n DEV 1 3| grep eth0 >> $lognetworkload

else
date +"%D %r" > $lognetworkload
sar -n DEV 1 3|head -3 |tail -1 >> $lognetworkload
sar -n DEV 1 3| grep eth0 >> $lognetworkload
fi

###################################
#统计系统进程总数信息
###################################
if [ -f $logsysprocess ]
then
date +"%D %r" >> $logsysprocess
sysnum=`ps aux | wc -l`
oranum=` ps -ef |awk '/ora/&&/dbticket/' |wc -l`
echo "processes of system is: $sysnum" >> $logsysprocess
echo "processes of oracle is: $oranum" >> $logsysprocess
else

date +"%D %r" > $logsysprocess
sysnum=`ps aux | wc -l`
oranum=` ps -ef |awk '/ora/&&/dbticket/' |wc -l`
echo "processes of system is: $sysnum" >> $logsysprocess
echo "processes of oracle is: $oranum" >> $logsysprocess


fi

##############################
#统计内存的信息
##############################
if [ -f $logmemuse ]
then
date +"%D %r" >> $logmemuse
free |grep -1 Mem |head -n 2|tail -1 >> $logmemuse
free | grep Swap >> $logmemuse
else
date +"%D %r" > $logmemuse
free |grep -1 Mem |head -n 2 >> $logmemuse
free | grep Swap >> $logmemuse
fi


###############################
#查看网络连接数，tcp协议
###############################
if [ -f $lognetsession ]
then
date +"%D %r" >> $lognetsession
netstat -an | grep -E "^(tcp)" | cut -c 74- | sort | uniq -c | sort -n >> $lognetsession
else
date +"%D %r" >> $lognetsession
netstat -an | grep -E "^(tcp)" | cut -c 74- | sort | uniq -c | sort -n >> $lognetsession
fi

###############################
#查看mysql processlist
###############################
if [ -f $logmysqlprocess ]
then
date +"%D %r" >> $logmysqlprocess
mysql -e 'show processlist;' >>$logmysqlprocess
else
date +"%D %r" >> $logmysqlprocess
mysql -e 'show processlist;' >>$logmysqlprocess
fi


